﻿#region Copyright
/* Product of DTL Software
 * Product Name : Support Memmory
 * Author : dinhtienlam.spkt@gmail.com
 * About: Developer of DTL Software
 * Contact : 0966 330 237
 */
#endregion

#region Using default
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Resources; 
#endregion

namespace Lamdt.Controls
{
    public partial class PageControl : UserControl
    {
        public delegate void ControlChangedHandler();
        [Category("Action")]
        [Description("New Lession")]
        public event ControlChangedHandler ControlChanged;
        protected virtual void PageControlChanged()
        {
            if (ControlChanged != null)
            {
                ControlChanged();
            }
        }
        Label lbl = new Label();
        private string _currentLanguage = "ja-JP";
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string CurrentLanguage { 
            set
            {
                _currentLanguage = value;
                Refresh();
            }
            get
            {
                return _currentLanguage;
            }
        }
        private int _currentPageIndex = 0;
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public int CurrentPageIndex
        {
            set
            {
                _currentPageIndex = value;
            }
            get
            {
                if (_currentPageIndex >= NumberPage && NumberPage > 0)
                {
                    _currentPageIndex = NumberPage - 1;
                }
                return _currentPageIndex;
            }
        }
        private bool _IsGridPaging = true;
        public bool IsGridPaging
        {
            set { _IsGridPaging = value; }
            get { return _IsGridPaging; }
        }
        private int _TotalRecord = 0;
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public int TotalRecord
        {
            get
            {
                return _TotalRecord;
            }
            set
            {
                _TotalRecord = value;
                Refresh();
            }
        }
        private List<LinkLabel> _linkLabelList = null;
        private List<LinkLabel> linkLabelList
        {
            get
            {
                if (_linkLabelList == null)
                {
                    _linkLabelList = new List<LinkLabel>();
                    _linkLabelList.Add(btn1);
                    _linkLabelList.Add(btn2);
                    _linkLabelList.Add(btn3);
                    _linkLabelList.Add(btn4);
                    _linkLabelList.Add(btn5);
                }
                return _linkLabelList;
            }
        }
        private int _NumberRowsPerPage = 20;
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public int NumberRowsPerPage
        {
            get
            {
                return _NumberRowsPerPage;
            }
            set
            {
                if (value <= 0)
                    throw new Exception("Value is invalid");
                _NumberRowsPerPage = value;
                Refresh();
                PageControlChanged();
            }
        }
        private int NumberPage
        {
            get
            {
                if (TotalRecord % NumberRowsPerPage == 0)
                    return TotalRecord / NumberRowsPerPage;
                return 1 + (TotalRecord / NumberRowsPerPage);
            }
        }
        private int NumberSegment
        {
            get
            {
                if (NumberPage % 5 == 0)
                    return NumberPage / 5;
                return 1 + (NumberPage / 5);
            }
        }
        private int CurrentSegmentIndex
        {
            get
            {
                return CurrentPageIndex / 5;
            }
        }
        private int CurrentPageIndexOfSegment
        {
            get
            {
                return CurrentPageIndex % 5;
            }
        }
        private int LastPageIndexOfLastSegment
        {
            get
            {
                return (NumberPage - 1) % 5;
            }
        }

        public override void Refresh()
        {
            if (TotalRecord == 0)
            {
                this.Visible = false;
                DisplayLabel();
                return;
            }
            this.Visible = true;
            for (int i = 0; i < 5; i++)
            {
                LinkLabel t = linkLabelList[i];
                if (i == CurrentPageIndexOfSegment)
                {
                    //Display of Current Page
                    t.Text = (CurrentSegmentIndex * 5 + i + 1) + " ";
                    t.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, (FontStyle.Bold | FontStyle.Underline));
                    t.Enabled = false;
                }
                else
                {
                    t.Text = (CurrentSegmentIndex * 5 + i + 1) + " ";
                    t.Enabled = true;
                    t.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, (FontStyle.Regular | FontStyle.Underline));
                }
            }
            //if current segment == last segment
            if (CurrentSegmentIndex == NumberSegment - 1)
            {
                btnGoLast.Enabled = false;
                for (int i = 0; i < 5; i++)
                {
                    if (i > LastPageIndexOfLastSegment)
                        linkLabelList[i].Visible = false;
                    else
                        linkLabelList[i].Visible = true;
                }
                if (CurrentPageIndexOfSegment == LastPageIndexOfLastSegment)
                    btnNext.Enabled = false;
                else
                    btnNext.Enabled = true;
            }
            else
            {
                for (int i = 0; i < 5; i++)
                {
                    linkLabelList[i].Visible = true;
                }
                btnGoLast.Enabled = true;
                btnNext.Enabled = true;
            }
            if (CurrentSegmentIndex == 0)
            {
                btnGoFirst.Enabled = false;
                if (CurrentPageIndexOfSegment == 0)
                    btnPrevious.Enabled = false;
                else
                    btnPrevious.Enabled = true;
            }
            else
            {
                btnGoFirst.Enabled = true;
                btnPrevious.Enabled = true;
            }
            if (btnGoFirst.Location.X < 0)
            {
                this.Location = new Point(this.Location.X + btnGoFirst.Location.X, this.Location.Y);
                this.Width += (-btnGoFirst.Location.X);
            }
            DisplayLabel();
            base.Refresh();
        }

        public PageControl()
        {
            InitializeComponent();
        }

        private void PageControl_Load(object sender, EventArgs e)
        {
            Refresh();
        }

        private void DisplayLabel()
        {
            if (IsGridPaging && this.Parent != null)
            {
                string result = string.Empty;
                if (TotalRecord == 0)
                {
                    if (CurrentLanguage == "ja-JP")
                        result = "レコードが見つかりませんでした。";
                    else
                        result = "There is no records !";
                }
                else
                {
                    if (CurrentLanguage == "ja-JP")
                        result = "全レコード数： " + _TotalRecord.ToString() + " ページ行数： " + _NumberRowsPerPage.ToString();
                    else
                        result = "Total Records: " + _TotalRecord.ToString() + "; Rows/Page: " + _NumberRowsPerPage.ToString();
                }
                lbl.AutoSize = true;
                lbl.Text = result;
                lbl.Anchor = AnchorStyles.Top;
                lbl.Location = new Point((this.Parent.Width - lbl.Width) / 2, this.Location.Y);
                lbl.BackColor = Color.Transparent;
                lbl.ForeColor = Constants.LamdtColor.DarkForeColor;
                lbl.Refresh();
                lbl.BringToFront();
                if (!this.Parent.Controls.Contains(lbl))
                    this.Parent.Controls.Add(lbl);
            }
        }

        private void btnPrevious_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            CurrentPageIndex--;
            Refresh();
            PageControlChanged();
        }

        private void btnGoLast_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            CurrentPageIndex = NumberPage;
            Refresh();
            PageControlChanged();
        }

        private void btnGoFirst_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            CurrentPageIndex = 0;
            Refresh();
            PageControlChanged();
        }

        private void btnNext_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            CurrentPageIndex++;
            Refresh();
            PageControlChanged();
        }

        private void btn_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            CurrentPageIndex = int.Parse((sender as LinkLabel).Text.Trim()) - 1;
            Refresh();
            PageControlChanged();
        }
    }
}
